package org.darkhelm.showsort.server.service;

import no.eirikb.gwtchannelapi.server.ChannelServer;

import org.darkhelm.showsort.shared.message.MessageEvent;
import org.darkhelm.showsort.shared.service.ChatService;

import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class ChatServiceImpl extends RemoteServiceServlet implements ChatService {

	private final String CHANNELNAME = "test";
	private static String channelKey;
	
	@Override
	public void sendMessage(String message) {
		ChannelServer.send(CHANNELNAME, new MessageEvent(message));

	}

	@Override
	public String join() {
		if (channelKey == null) {
			channelKey = ChannelServiceFactory.getChannelService().createChannel(CHANNELNAME);
		}
		return channelKey;
	}

}
